package com.das.mechanic_alone.mvp.view.alone;

import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.view.KeyEvent;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.das.mechanic_alone.mvp.contract.alone.X3WebAloneServiceContract;
import com.das.mechanic_alone.mvp.presenter.alone.X3WebAloneServicePresenter;
import com.das.mechanic_base.R;
import com.das.mechanic_base.R2;
import com.das.mechanic_base.adapter.alone.X3NewAloneSerViceSetAdapter;
import com.das.mechanic_base.adapter.alone.X3NewAloneServiceItemAdapter;
import com.das.mechanic_base.adapter.alone.X3NewAloneTitleAdapter;
import com.das.mechanic_base.app.X3AppRouterConstants;
import com.das.mechanic_base.app.X3Constent;
import com.das.mechanic_base.base.X3BaseActivity;
import com.das.mechanic_base.bean.alone.NewServiceHeaderBean;
import com.das.mechanic_base.bean.camera.CameraBean;
import com.das.mechanic_base.bean.even.DeleteFriends;
import com.das.mechanic_base.bean.greendao.AloneServiceListBean;
import com.das.mechanic_base.bean.main.HomeMainCarBean;
import com.das.mechanic_base.utils.X3AloneServiceSearchUtils;
import com.das.mechanic_base.utils.X3FileUtils;
import com.das.mechanic_base.utils.X3StatusBarUtil;
import com.das.mechanic_base.utils.X3StringUtils;
import com.das.mechanic_base.utils.X3ToastUtils;
import com.das.mechanic_base.widget.X3BottomAffirmMileDialog;
import com.das.mechanic_base.widget.X3BottomAlreadyAloneServiceDialog;
import com.das.mechanic_base.widget.X3NewBottomMileOrTimeDialog;
import com.das.mechanic_base.widget.X3PointDialog;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.umeng.analytics.MobclickAgent;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

@Route(path = X3AppRouterConstants.Router.WEB_ALONE_SERVICE)
public class X3WebAloneServiceActivity extends X3BaseActivity<X3WebAloneServicePresenter> implements X3WebAloneServiceContract.View, X3NewAloneSerViceSetAdapter.IOnClick, X3NewAloneTitleAdapter.IOnShowTilte, X3NewAloneServiceItemAdapter.IOnClickService, DialogInterface.OnDismissListener, X3NewBottomMileOrTimeDialog.IOnAffirmSelect {

    @BindView(R2.id.rlv_header)
    RecyclerView rlv_header;
    @BindView(R2.id.rlv_second)
    RecyclerView rlv_second;
    @BindView(R2.id.rlv_service)
    RecyclerView rlv_service;
    @BindView(R2.id.rl_header)
    RelativeLayout rl_header;
    @BindView(R2.id.tv_shop_num)
    TextView tv_shop_num;
    @BindView(R2.id.rl_shop)
    RelativeLayout rl_shop;
    @BindView(R2.id.tv_btn)
    TextView tv_btn;
    @BindView(R2.id.rl_loading)
    RelativeLayout rl_loading;

    private boolean isEdit = false;//是否删除或者添加

    private X3NewAloneSerViceSetAdapter setAdapter;
    private long workBaseId;
    private X3NewAloneTitleAdapter titleAdapter;
    private X3NewAloneServiceItemAdapter itemAdapter;
    private List<NewServiceHeaderBean.AllSerViceListBean> mList;
    private List<NewServiceHeaderBean.AllSerViceListBean.ListBeanX> sList;

    private float percent;
    private List<NewServiceHeaderBean.AlreadyInPlanListBean> cacheList;
    private long questionBaseId;
    private String decTitle;
    private X3BottomAlreadyAloneServiceDialog alreadyAloneServiceDialog;
    private NewServiceHeaderBean.AlreadyInPlanListBean changeServiceBean;
    private HashMap<String, Integer> titleMap;

    private long lastVisibleIndex = 0;
    private X3PointDialog delDialog;
    private X3NewBottomMileOrTimeDialog mileOrTimeDialog;
    private X3BottomAffirmMileDialog mileDialog;
    private NewServiceHeaderBean.AlreadyInPlanListBean serviceListBean;
    private boolean needUpdateMile;
    private double newPerMiles;
    private double newCurrentMile;
    private long questionOptionId;
    private HomeMainCarBean carBean;
    private Map<String, AloneServiceListBean> aloneMap;
    private List<NewServiceHeaderBean.AlreadyInPlanListBean> newCacheList;
    private boolean isClickBtn = false;
    private long carId;

    @Override
    protected int getLayoutId() {
        return R.layout.x3_activity_alone_service_web;
    }

    @Override
    public boolean isTransparent() {
        return true;
    }

    @Override
    protected void initView() {
        if (android.os.Build.VERSION.SDK_INT != android.os.Build.VERSION_CODES.O) {
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        }
        X3StatusBarUtil.darkMode(this);
        swipeBackLayout.setEnableGesture(false);
        rl_shop.setVisibility(View.GONE);
        carBean = (HomeMainCarBean) getIntent().getSerializableExtra("carBean");
        workBaseId = getIntent().getIntExtra("workBaseId", 0);
        questionBaseId = getIntent().getIntExtra("questionBaseId", 0);
        decTitle = getIntent().getStringExtra("decTitle");
        questionOptionId = getIntent().getIntExtra("questionOptionId", 0);

        rlv_header.setLayoutManager(new LinearLayoutManager(this,RecyclerView.HORIZONTAL,false));
        rlv_second.setLayoutManager(new LinearLayoutManager(this,RecyclerView.HORIZONTAL,false));
        rlv_service.setLayoutManager(new LinearLayoutManager(this));

        needUpdateMile = getIntent().getBooleanExtra("needUpdateMile", false);

        rlv_header.requestDisallowInterceptTouchEvent(true);
        setAdapter = new X3NewAloneSerViceSetAdapter(this);
        rlv_header.setAdapter(setAdapter);
        setAdapter.setiOnClick(this);

        titleAdapter = new X3NewAloneTitleAdapter(this);
        rlv_second.setAdapter(titleAdapter);
        titleAdapter.setiOnShowTilte(this);

        carId = getIntent().getLongExtra("carId", 0L);
        itemAdapter = new X3NewAloneServiceItemAdapter(this);
        rlv_service.setAdapter(itemAdapter);
        itemAdapter.setiOnClickService(this);
        itemAdapter.changeCarId(carId);
        changeButtonStustas();
        rlv_service.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                if (isFinishing()) {
                    return;
                }
                int visiblePosition = ((LinearLayoutManager) rlv_service.getLayoutManager()).findFirstVisibleItemPosition();
                if (titleMap == null) {
                    return;
                }
                int visibleIndex = -1;
                for (String key : titleMap.keySet()) {
                    if (visiblePosition >= titleMap.get(key)) {
                        visibleIndex++;
                    }
                }
                if (visibleIndex == -1) {
                    return;
                }
                if (lastVisibleIndex == visibleIndex) {
                    return;
                }
                if (titleAdapter != null) {
                    titleAdapter.changeTitleSelectList(visibleIndex);
                    lastVisibleIndex = visibleIndex;
                }
            }
        });
        if (mPresenter != null) {
            rl_loading.setVisibility(View.VISIBLE);
            mPresenter.getServiceBaseInfo(carId,false);
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onPageStart("检测系统添加服务项目弹窗");
//        searchAloneCache();
    }

    /**
     * 搜索本地缓存中添加的服务项目
     */
    @Override
    public void searchAloneCache() {
        if(isClickBtn){
            Intent intent = getIntent();
            setResult(RESULT_OK,intent);
            finish();
            return;
        }
        if (mPresenter != null) {
            mPresenter.getDetectionServiceList();
        }
    }

    @Override
    public void saveCarMaintenancePlanSus() {//保存
        EventBus.getDefault().post(new DeleteFriends("REFRESH_GROUND"));
        finish();
    }

    @Override
    protected X3WebAloneServicePresenter createPresenter() {
        return new X3WebAloneServicePresenter();
    }

    @OnClick({R2.id.iv_back,R2.id.rl_shop,R2.id.tv_search,R2.id.tv_btn})
    public void onViewClick(View view) {
        int id = view.getId();
        if (id == R.id.iv_back) {
            showDilaog();
        } else if (id == R.id.rl_shop) {
            if (alreadyAloneServiceDialog == null) {
                alreadyAloneServiceDialog = new X3BottomAlreadyAloneServiceDialog(this);
            }
            alreadyAloneServiceDialog.show();
            alreadyAloneServiceDialog.setOnDismissListener(this);
            alreadyAloneServiceDialog.getCurrentDetectionList(providerWorkBaseId(), needUpdateMile, carBean);
        } else if (id == R.id.tv_search) {/**
         * 跳转到模糊搜索服务项目
         */
            Intent intent = new Intent(this, X3NewAloneSearchActivity.class);
            intent.putExtra("workBaseId", providerWorkBaseId());
            intent.putExtra("questionBaseId", questionBaseId);
            intent.putExtra("decTitle", decTitle);
            intent.putExtra("needUpdateMile", needUpdateMile);
            intent.putExtra("questionOptionId", questionOptionId);
            intent.putExtra("carId", carId);
            startActivityForResult(intent, 1);
        } else if (id == R.id.tv_btn) {
            if (!isEdit) {
                X3ToastUtils.showMessage(getString(R.string.add_maintance));
                return;
            }
            isClickBtn = true;
            NewServiceHeaderBean newServiceHeaderBean = new NewServiceHeaderBean();
            newServiceHeaderBean.alreadyInPlanList = newCacheList;
            newServiceHeaderBean.carId = carId;
            mPresenter.saveCarMaintenancePlan(newServiceHeaderBean);
        }
    }

    @Override
    public long providerWorkBaseId() {
        return workBaseId;
    }

    @Override
    public void getServiceInfo(NewServiceHeaderBean bean, boolean isJump) {
        rl_loading.setVisibility(View.GONE);
        if(bean == null){
            return;
        }
        if (X3StringUtils.isListEmpty(bean.allSerViceList)) {
            mList = new ArrayList<>();
        }
        this.mList = bean.allSerViceList;
        this.cacheList = bean.alreadyInPlanList;
        newCacheList = bean.alreadyInPlanList;

        if (isJump && setAdapter != null) {
            sList = setAdapter.getSelectAloneService();
            if (titleAdapter != null) {
                titleAdapter.changeTitleList(sList);
            }

            initItemAdapter(sList);
            return;
        }

        if (setAdapter != null) {
            setAdapter.changeServiceSet(mList);
        }

        if (mList.size() <= 0) {
            sList = new ArrayList<>();
        }else {
            sList = mList.get(0).list;
        }

        if (titleAdapter != null) {
            titleAdapter.changeTitleList(sList);
        }

        initItemAdapter(sList);
    }

    /**
     * 初始化服务项目条目信息
     * @param sList
     */
    private void initItemAdapter(List<NewServiceHeaderBean.AllSerViceListBean.ListBeanX> sList) {
        List<NewServiceHeaderBean.AlreadyInPlanListBean> baseList = new ArrayList<>();
        titleMap = new HashMap<>();
        if (!X3StringUtils.isListEmpty(sList)) {
            for (int i = 0; i < sList.size(); i++) {
                if (!titleMap.containsKey(sList.get(i).shopCategoryName)) {
                    titleMap.put(sList.get(i).shopCategoryName,baseList.size());
                }
                List<NewServiceHeaderBean.AlreadyInPlanListBean> serviceBaseEntityList = sList.get(i).list;

                if (X3StringUtils.isListEmpty(serviceBaseEntityList)) {
                    break;
                }
                baseList.addAll(serviceBaseEntityList);
            }
        }

        if (itemAdapter != null) {
            itemAdapter.changeServiceItem(baseList);
        }
    }

    @Override
    public void iOnClickItem(int position) {
        if (position >= mList.size()) {
            sList = new ArrayList<>();
        }else {
            sList = mList.get(position).list;
        }
        if (X3StringUtils.isListEmpty(sList)) {
            sList = new ArrayList<>();
        }

        if (titleAdapter != null) {
            titleAdapter.changeTitleList(sList);
        }

        // 重置滑动到顶部
        ((LinearLayoutManager)rlv_service.getLayoutManager()).scrollToPositionWithOffset(0,0);
        ((LinearLayoutManager)rlv_service.getLayoutManager()).setStackFromEnd(true);
        initItemAdapter(sList);
    }

    @Override
    public void iOnShowTitleItem(String title) {
        if (titleMap == null) {
            return;
        }
        int index = titleMap.get(title);
        ((LinearLayoutManager)rlv_service.getLayoutManager()).scrollToPositionWithOffset(index,0);
        ((LinearLayoutManager)rlv_service.getLayoutManager()).setStackFromEnd(true);
    }

    @Override
    public void iOnCuteService(boolean isCute, NewServiceHeaderBean.AlreadyInPlanListBean serviceBean,int position) {

        serviceListBean = X3AloneServiceSearchUtils.searchNewAloneServiceBean(cacheList,serviceBean.serviceBaseSn);

        if (isCute) {
            /**
             * 将这个服务项目从缓存中去除
             */
            if (serviceListBean == null) {
                searchAloneCache();
                return;
            }
            cacheList = X3AloneServiceSearchUtils.replaceNewAloneServiceBean(cacheList,serviceListBean.serviceBaseSn,serviceListBean);
            Iterator<NewServiceHeaderBean.AlreadyInPlanListBean> iterator = cacheList.iterator();
            while (iterator.hasNext()) {
                NewServiceHeaderBean.AlreadyInPlanListBean next = iterator.next();
                if (next.serviceBaseSn.equals(serviceListBean.serviceBaseSn)) {
                    iterator.remove();
                }
            }
            newCacheList = cacheList;
            isEdit = true;
            changeButtonStustas();

        }else {
            if (!needUpdateMile) {
                serviceListBean = addServiceItem(serviceBean, serviceListBean);
                iOnShowChangeServiceOne(serviceListBean, serviceListBean.reductionType);
            }else {
                // 否则需要先确认总里程和日均里程
                mileDialog = new X3BottomAffirmMileDialog(this);
                mileDialog.show();
                mileDialog.setWorkBaseId(workBaseId,null);
                mileDialog.setiOnClickAffirmMiles((currentMiles1, perMiles1) -> {
                    // 确认完总里程和日均里程了
                    needUpdateMile = false;

                    newPerMiles = Double.parseDouble(X3StringUtils.formatDouble(perMiles1));
                    newCurrentMile = Double.parseDouble(X3StringUtils.formatDouble(currentMiles1));

                    CameraBean cameraBean = new CameraBean();
                    cameraBean.currentMiles = newCurrentMile;
                    cameraBean.perMiles = newPerMiles;
                    EventBus.getDefault().post(new CameraBean(X3Constent.CODE_ALONE_CHANGE_NEED_UPDATE,cameraBean));

                    serviceBean.currentMiles = newCurrentMile;
                    serviceBean.perMiles = newPerMiles;

                    serviceListBean = addServiceItem(serviceBean, serviceListBean);

                    if (mPresenter != null) {
                        mPresenter.getDetectionServiceList();
                    }
                    if (itemAdapter != null) {
                        itemAdapter.refreshMiles(newCurrentMile, newPerMiles);
                    }

                    iOnShowChangeServiceOne(serviceListBean,serviceListBean.reductionType);
                });
            }
        }
    }

    private NewServiceHeaderBean.AlreadyInPlanListBean addServiceItem(NewServiceHeaderBean.AlreadyInPlanListBean serviceBean,NewServiceHeaderBean.AlreadyInPlanListBean serviceListBean1) {
        NewServiceHeaderBean.AlreadyInPlanListBean newServiceBean = serviceListBean1;
        if (newServiceBean == null) {
            newServiceBean = new NewServiceHeaderBean.AlreadyInPlanListBean();
            newServiceBean.nextReplaceCycleKm = serviceBean.nextReplaceCycleKm;
            newServiceBean.serviceBaseName = serviceBean.serviceBaseName;
            newServiceBean.serviceBaseSn = serviceBean.serviceBaseSn;
            newServiceBean.currentMiles = serviceBean.currentMiles;
            newServiceBean.perMiles = serviceBean.perMiles;
            newServiceBean.seleced = serviceBean.seleced;
            newServiceBean.carId = serviceBean.carId;
            newServiceBean.reductionType = serviceBean.reductionType;
            newServiceBean.warningDate = serviceBean.warningDate;

            cacheList.add(newServiceBean);
        }else {
            cacheList = X3AloneServiceSearchUtils.replaceNewAloneServiceBean(cacheList,newServiceBean.serviceBaseSn,newServiceBean);
        }
        return newServiceBean;
    }

    @Override
    public void iOnShowChangeService(String sn,String remindType,double currentMiles,double perMiles) {
        changeServiceBean = X3AloneServiceSearchUtils.searchNewAloneServiceBean(cacheList,sn);
        changeServiceBean.currentMiles = currentMiles;
        changeServiceBean.perMiles = perMiles;

        modifyMileOrTime(changeServiceBean,remindType);
    }


    public void iOnShowChangeServiceOne(NewServiceHeaderBean.AlreadyInPlanListBean changeServiceBean,String remindType) {
        this.changeServiceBean = changeServiceBean;
        modifyMileOrTime(changeServiceBean,remindType);
    }

    private void modifyMileOrTime(NewServiceHeaderBean.AlreadyInPlanListBean changeServiceBean,String remindType) {
        if (changeServiceBean.currentMiles <= 0 && changeServiceBean.currentMiles <= 1) {
            changeServiceBean.perMiles = newPerMiles;
            changeServiceBean.currentMiles = newCurrentMile;
        }
        AloneServiceListBean aloneServiceListBean = new AloneServiceListBean();
        aloneServiceListBean.setPerMiles(changeServiceBean.perMiles);
        aloneServiceListBean.setCurrentMiles(changeServiceBean.currentMiles);
        aloneServiceListBean.setRecommendSafeMiles(changeServiceBean.nextReplaceCycleKm);
        aloneServiceListBean.setTouchServiceSn(changeServiceBean.serviceBaseSn);
        aloneServiceListBean.setWarningDate(changeServiceBean.warningDate);
        aloneServiceListBean.setReductionType(changeServiceBean.reductionType);
        aloneServiceListBean.setCarId(changeServiceBean.carId);
        aloneServiceListBean.setSeleced(true);
        aloneServiceListBean.setTouchServiceBaseName(changeServiceBean.serviceBaseName);

        mileOrTimeDialog = new X3NewBottomMileOrTimeDialog(this,2);
        mileOrTimeDialog.show();
        mileOrTimeDialog.showCurrentMile(aloneServiceListBean,remindType);
        mileOrTimeDialog.setiOnAffirmSelect(this);
    }


    @Override
    public void iOnShowDelete(int position, String sn) {
        NewServiceHeaderBean.AlreadyInPlanListBean serviceListBean = X3AloneServiceSearchUtils.searchNewAloneServiceBean(cacheList,sn);
        if (serviceListBean == null) {
            return;
        }
        String s = X3FileUtils.loadDataFromFile(this, providerWorkBaseId() + serviceListBean.serviceBaseSn);
        aloneMap = null;
        if (!X3StringUtils.isEmpty(s)) {
            aloneMap = new Gson().fromJson(s,new TypeToken<Map<String, AloneServiceListBean>>(){}.getType());
        }

            delDialog = new X3PointDialog(this,"",getString(R.string.x3_delete_add_service),getString(R.string.x3_cancel_info),getString(R.string.x3_affirm_info));
            delDialog.show();
            delDialog.setRightClick(view -> {
                delDialog.dismiss();
                if (itemAdapter != null) {
                    itemAdapter.deleteService(position,true,true);
                }
            });

    }

    @Override
    public void iOnAffirmMiles(AloneServiceListBean listBean) {
        isEdit = true;
        NewServiceHeaderBean.AlreadyInPlanListBean alreadyInPlanListBean = new NewServiceHeaderBean.AlreadyInPlanListBean();
        alreadyInPlanListBean.serviceBaseSn =listBean.getTouchServiceSn();
        alreadyInPlanListBean.currentMiles = listBean.getCurrentMiles();
        alreadyInPlanListBean.perMiles = listBean.getPerMiles();
        alreadyInPlanListBean.carId = listBean.getCarId();
        alreadyInPlanListBean.warningDate = listBean.getWarningDate();
        alreadyInPlanListBean.nextReplaceCycleKm = listBean.getRecommendSafeMiles();
        alreadyInPlanListBean.reductionType = listBean.getReductionType();
        alreadyInPlanListBean.seleced = listBean.isSeleced();

        boolean isHave = false;
        newCacheList = cacheList;
        for (int i = 0; i < newCacheList.size(); i++) {
            NewServiceHeaderBean.AlreadyInPlanListBean aloneServiceListBean = newCacheList.get(i);
            if (aloneServiceListBean.serviceBaseSn.equals(listBean.getTouchServiceSn())) {
                isHave = true;
                newCacheList.remove(i);
                newCacheList.add(i,alreadyInPlanListBean);
                break;
            }
        }
        if (!isHave) {
            newCacheList.add(alreadyInPlanListBean);
        }

        changeButtonStustas();
        if (itemAdapter != null) {
            itemAdapter.changeMilesService(alreadyInPlanListBean);
        }
    }
    @Override
    public void iOnPickerDismiss() {
        CameraBean cameraBean = new CameraBean(X3Constent.CODE_ALONE_ADD_SERVICE_REQUEST,cacheList);
        EventBus.getDefault().post(cameraBean);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void onDismiss(DialogInterface dialogInterface) {
        Map<String,Object> map = new HashMap<>();
        map.put("workBaseId", workBaseId);
        map.put("questionBaseId",questionBaseId);
        map.put("questionOptionId",questionOptionId);
        if (mPresenter != null) {
            mPresenter.getServiceBaseInfo(carId,false);
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd("检测系统添加服务项目弹窗");
    }

    public void changeButtonStustas(){
        if(isEdit){//编辑过
            tv_btn.setBackgroundResource(R.drawable.x3_bg_btn_confirm);
        }else {
            tv_btn.setBackgroundResource(R.drawable.x3_bg_btn_confirm_w);
        }
    }

    public void showDilaog(){
        if(isEdit) {
            X3PointDialog dialog = new X3PointDialog(this, "", getString(R.string.give_up_refresh_maintance), getString(R.string.x3_cancel_info), getString(R.string.x3_affirm_info));
            dialog.show();
            dialog.setRightClick(view -> {
                dialog.dismiss();
                Intent intent = getIntent();
                setResult(RESULT_OK,intent);
                finish();
                overridePendingTransition(0, R.anim.activity_exit);
            });

        }else {
            Intent intent = getIntent();
            setResult(RESULT_OK,intent);
            finish();
            overridePendingTransition(0, R.anim.activity_exit);
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
            showDilaog();
            return true;
        }
        Intent intent = getIntent();
        setResult(RESULT_OK,intent);
        finish();
        return super.onKeyDown(keyCode, event);
    }

    public boolean completeList(List<NewServiceHeaderBean.AlreadyInPlanListBean> listOne,List<NewServiceHeaderBean.AlreadyInPlanListBean> listTwo) {
        if (X3StringUtils.isListEmpty(listOne) && X3StringUtils.isListEmpty(listTwo)) {
            return true;
        } else if (X3StringUtils.isListEmpty(listOne) && !X3StringUtils.isListEmpty(listTwo)) {
            return false;
        } else if (!X3StringUtils.isListEmpty(listOne) && X3StringUtils.isListEmpty(listTwo)) {
            return false;
        } else {
            for (int i = 0; i < listOne.size(); i++) {
                if (!listTwo.contains(listOne.get(i).serviceBaseSn)) {
                    return false;
                }
            }
        }
      return true;
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode == 1){
            if (data == null) {
                return;
            }
            List<NewServiceHeaderBean.AlreadyInPlanListBean> resultList = (List<NewServiceHeaderBean.AlreadyInPlanListBean>) data.getSerializableExtra("resultList");

            if(X3StringUtils.isListEmpty(resultList)){
                return;
            }

            if(itemAdapter != null){
                List<NewServiceHeaderBean.AlreadyInPlanListBean> list = itemAdapter.getmList();
                for (int i =0;i < resultList.size();i++){
                    NewServiceHeaderBean.AlreadyInPlanListBean resultBean = resultList.get(i);
                    for (int j = 0;j < list.size();j++){
                        NewServiceHeaderBean.AlreadyInPlanListBean listBean = list.get(j);
                        if (!resultBean.serviceBaseSn.equals(listBean.serviceBaseSn)) {
                            continue;
                        }
                        list.remove(j);
                        listBean.serviceBaseSn = resultList.get(i).serviceBaseSn;
                        listBean.perMiles =resultList.get(i).perMiles;
                        listBean.currentMiles = resultList.get(i).currentMiles;
                        listBean.serviceBaseName =resultList.get(i).serviceBaseName;
                        listBean.nextReplaceCycleKm = resultList.get(i).nextReplaceCycleKm;
                        listBean.reductionType = resultList.get(i).reductionType;
                        listBean.seleced = resultList.get(i).seleced;
                        listBean.carId = resultList.get(i).carId;
                        listBean.warningDate = resultList.get(i).warningDate;
                        list.add(j,listBean);
                    }
                }


                    Iterator<NewServiceHeaderBean.AlreadyInPlanListBean> iterator = cacheList.iterator();
                    while (iterator.hasNext()) {
                        NewServiceHeaderBean.AlreadyInPlanListBean next = iterator.next();
                        for (int j = 0; j < resultList.size();j++){
                            if(X3StringUtils.isEmpty(next.serviceBaseSn)){
                                continue;
                            }
                            if(next.serviceBaseSn.equals(resultList.get(j).serviceBaseSn)){
                                iterator.remove();
                                break;
                            }
                        }

                    }
                cacheList.addAll(resultList);
                newCacheList = cacheList;
                isEdit = true;
                changeButtonStustas();
                if (itemAdapter != null) {
                    itemAdapter.changeServiceItem(list);
                }
            }
        }
    }
}